package com.lzxr.test.servlet;

import com.lzxr.test.entity.R;
import com.lzxr.test.entity.User;
import com.lzxr.test.exception.UserException;
import com.lzxr.test.service.UserService;
import com.lzxr.test.service.impl.UserServiceImpl;
import com.lzxr.test.utils.enums.UserEnum;
import java.io.IOException;
import java.util.Optional;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    public UserServlet() {
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("utf-8");

        // 验证码检验
        String code = req.getParameter("code");
        String rightCode = (String)Optional.ofNullable(req.getSession().getAttribute("code")).orElse("");
        if (!rightCode.toLowerCase().equals(code.toLowerCase())) {
            resp.getWriter().print(R.error("验证码检验失败"));
        } else {
            switch (req.getParameter("action")) {
                case "login":
                    this.doLogin(req, resp);
                    break;
                case "reg":
                    this.doRegiter(req, resp);
            }

        }
    }

    private User getUser(HttpServletRequest req) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        return new User(username, password);
    }

    private void doRegiter(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = this.getUser(req);
        boolean reg = false;

        try {
            reg = this.userService.reg(user);
        } catch (UserException var10) {
            // 根据自定义的异常信息，向前端返回统一的数据
            UserEnum userEnum = var10.getUserEnum();
            Integer code = userEnum.getCode();
            String msg = userEnum.getMsg();
            R r = new R(code, msg);
            resp.getWriter().print(r);
        }

        if (reg) {
            resp.getWriter().print(R.ok("注册成功"));
        }

    }

    private void doLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = this.getUser(req);
        User ret = null;

        try {
            ret = this.userService.login(user);
        } catch (UserException var10) {
            UserEnum userEnum = var10.getUserEnum();
            Integer code = userEnum.getCode();
            String msg = userEnum.getMsg();

            R r = new R(code, msg);
            resp.getWriter().print(r);
            return;
        }

        // 登录成功使用session记录下
        req.getSession().setAttribute("isLogin", "true");
        R r = new R(0, "登录成功");
        r.setData(ret);
        resp.getWriter().print(r);
    }
}
